package zj.com.activities;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.xutils.DbManager;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import zj.com.adapter.DjcAdapter;
import zj.com.adapter.DqrAdapter;
import zj.com.adapter.FragmentAdapter;
import zj.com.app.ZjApplication;
import zj.com.db.DBUtil;
import zj.com.fragments.DqrLeftFragment;
import zj.com.fragments.DqrRightFragment;
import zj.com.fragments.JcdInfoFragment;
import zj.com.fragments.JcxFragment;
import zj.com.model.Dic;
import zj.com.model.DjcModel;
import zj.com.model.InspectContent;
import zj.com.model.InspectItem;
import zj.com.model.JcdInfoDB;
import zj.com.model.JcxDB;
import zj.com.model.JcxDetailDB;
import zj.com.model.UserInfo;
import zj.com.net.NetManager;
import zj.com.net.OnResponseListener;
import zj.com.util.DialogUtil;
import zj.com.util.LogUtils;
import zj.com.zj.R;

/**
 * Created by xi on 16/6/3.
 * 待确认
 */
public class DqrListActivity extends FragmentActivity implements View.OnClickListener{


    public TextView title_name;
    public ImageView left_iv;
    public TextView right_tv;

    private List<Fragment> mFragmentList = new ArrayList<Fragment>();
    private FragmentAdapter mFragmentAdapter;
    private int screenWidth;
    private ViewPager mPageVp;
    /**
     * Tab显示内容TextView
     */
    private TextView mjcxTv, mjcdTv;
    /**
     * Tab的那个引导线
     */
    private ImageView mTabLineIv;

    private  DqrLeftFragment jcdInfoFragment;
    public   DqrRightFragment      jcxFragment;
    private int currentIndex;
    public int  mode;

    public LinearLayout id_tab_friend_ll,id_tab_chat_ll;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jcd_edit);
        initView();
        init();
        initTabLineWidth();
    }





    public void initView(){

        id_tab_chat_ll = (LinearLayout) findViewById(R.id.id_tab_chat_ll);
        id_tab_chat_ll.setOnClickListener(this);
        id_tab_friend_ll = (LinearLayout) findViewById(R.id.id_tab_friend_ll);
        id_tab_friend_ll.setOnClickListener(this);

        title_name = (TextView) findViewById(R.id.title_name);
        title_name.setText("检查确认任务");


        left_iv = (ImageView) findViewById(R.id.left_iv);
        left_iv.setOnClickListener(this);
        left_iv.setVisibility(View.VISIBLE);
        right_tv = (TextView) findViewById(R.id.right_tv);
        right_tv.setText("保存");
        right_tv.setVisibility(View.GONE);
        right_tv.setOnClickListener(this);

        mjcdTv = (TextView) this.findViewById(R.id.id_jcd_tv);
        mjcxTv = (TextView) this.findViewById(R.id.id_jcx_tv);
        mjcdTv.setText("待确认任务");
        mjcxTv.setText("待确认陪同检查");
        mTabLineIv = (ImageView) this.findViewById(R.id.id_tab_line_iv);

        mPageVp = (ViewPager) this.findViewById(R.id.id_page_vp);
    }

    @Override
    protected void onResume() {
        super.onResume();

//        if(jcdInfoFragment != null){
//            jcdInfoFragment.setUserVisibleHint(true);
//        }

        LogUtils.getInstance().error("========dqr list onresume==========");
    }

    private void init() {
        jcdInfoFragment = new DqrLeftFragment();
        jcxFragment = new DqrRightFragment();
        mFragmentList.add(jcdInfoFragment);
        mFragmentList.add(jcxFragment);

        mFragmentAdapter = new FragmentAdapter(
                this.getSupportFragmentManager(), mFragmentList);
        mPageVp.setAdapter(mFragmentAdapter);
        mPageVp.setCurrentItem(0);

        mPageVp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            /**
             * state滑动中的状态 有三种状态（0，1，2） 1：正在滑动 2：滑动完毕 0：什么都没做。
             */
            @Override
            public void onPageScrollStateChanged(int state) {

            }

            /**
             * position :当前页面，及你点击滑动的页面 offset:当前页面偏移的百分比
             * offsetPixels:当前页面偏移的像素位置
             */
            @Override
            public void onPageScrolled(int position, float offset,
                                       int offsetPixels) {
                LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mTabLineIv
                        .getLayoutParams();

                Log.e("wxf", "postion=" + position + "    offset=" + offset + " =====currentIndex=" + currentIndex);

                if (currentIndex == 0 && position == 0)// 0->1
                {
//                    lp.leftMargin = (int) (offset * (screenWidth * 1.0) + currentIndex
//                            * (screenWidth / 2));
                    lp.leftMargin =
                            0;
                    Log.e("wxf", "0  0");


                } else if (currentIndex == 1 && position == 0) {
                    Log.e("wxf", "1  0");
                    lp.leftMargin =
                            screenWidth / 2;
                } else if (currentIndex == 1 && position == 1) {

                    Log.e("wxf", "1  1");
                    lp.leftMargin =
                            screenWidth / 2;
                }
                // mTabLineIv.setLayoutParams(lp);
            }

            @Override
            public void onPageSelected(int position) {
                resetTextView();
                LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mTabLineIv
                        .getLayoutParams();
                switch (position) {
                    case 0:
                        mjcdTv.setTextColor(getResources().getColor(R.color.color_text_p));

                        lp.leftMargin = 0;
                        break;
                    case 1:


                        lp.leftMargin =
                                screenWidth / 2;
                        mjcxTv.setTextColor(getResources().getColor(R.color.color_text_p));
                        ;
                        break;

                }
                mFragmentList.get(position).onResume();
                mTabLineIv.setLayoutParams(lp);
                currentIndex = position;
            }
        });

    }

    /**
     * 设置滑动条的宽度为屏幕的1/3(根据Tab的个数而定)
     */
    private void initTabLineWidth() {
        DisplayMetrics dpMetrics = new DisplayMetrics();
        getWindow().getWindowManager().getDefaultDisplay()
                .getMetrics(dpMetrics);
        screenWidth = dpMetrics.widthPixels;
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mTabLineIv
                .getLayoutParams();
        lp.width = screenWidth / 2;
        mTabLineIv.setLayoutParams(lp);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        jcdInfoFragment.onActivityResult(requestCode,resultCode,data);
        super.onActivityResult(requestCode, resultCode, data);
    }

    /**
     * 重置颜色
     */
    private void resetTextView() {
        mjcxTv.setTextColor(Color.parseColor("#828282"));
        mjcdTv.setTextColor(Color.parseColor("#828282"));
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.left_iv:
                finish();
                break;
            case R.id.right_tv:
                break;
            case R.id.id_tab_friend_ll:
                mPageVp.setCurrentItem(1);
                break;
            case R.id.id_tab_chat_ll:
                mPageVp.setCurrentItem(0);

                break;

        }
    }


}




